package com.translation.verb;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.translation.dao.BaseDAO;
import com.translation.dao.Batch;

public class SpaceImporter {

	public static void main(String[] args) throws IOException, SQLException {
		BufferedReader r = null;
		try {
			r = new BufferedReader(new FileReader(args[0]));
			final BufferedReader r1 = r;
			Iterator<String[]> it = new Iterator<String[]>() {
				Pattern p = Pattern.compile("^([^0-9\\s][^;]*);([^\\\\;]*);(\\\\N|[0-9]+).*");
				String word = null;
				String[] tokens = null;
				int index = -1;

				@Override
				public void remove() {
				}

				@Override
				public String[] next() {
					String line = null;
					String token = null;

					try {

						if (word != null) {
							index++;
							if (index >= 0 && index < tokens.length) {
								token = tokens[index];
							} else {
								index = -1;
								word = null;
								tokens = null;
							}
						}

						while (word == null) {
							if ((line = r1.readLine()) != null) {
								Matcher m = p.matcher(line);
								if (m.find()) {
									word = m.group(1).trim();
									if (word.equals("")) {
										word = null;
										tokens = null;
										index = -1;
									} else {
										String mean = m.group(2);
										tokens = mean.split("[\\s]+");
										index = 0;
										token = tokens[index];
									}
								}
							} else {
								break;
							}
						}

					} catch (Exception e) {
					}

					return (word != null) ? new String[]{
															word,
															token} : null;
				}

				@Override
				public boolean hasNext() {
					return true;
				}
			};

			/*
			String[] entry = null;
			while ((entry = it.next()) != null) {
				System.out.println(Arrays.toString(entry));
			}
			 */

			Batch batch = getBatch(it, args[1]);
			BaseDAO dao = new BaseDAO();
			dao.namedBatch("space", batch);

		} finally {
			if (r != null) {
				r.close();
			}
		}
	}
	private static Batch getBatch(final Iterator<String[]> it, final String locale_id) {
		return new Batch() {

			private String[] word;

			@Override
			public void setParams(PreparedStatement st) throws SQLException {
				int index = 1;
				System.out.println(word[0] + ":" + word[1]);
				st.setString(index++, word[0]);
				st.setString(index++, word[1]);
				st.setString(index++, "transl.");
				st.setString(index++, locale_id);
			}

			@Override
			public boolean result(int[] result, PreparedStatement st) throws SQLException {
				return true;
			}

			@Override
			public boolean next() throws SQLException {
				word = it.next();
				return word != null;
			}

			@Override
			public int getSize() {
				return 100;
			}
			@Override
			public void error(SQLException e) {
				if (word != null) {
					System.out.println("Near:" + Arrays.toString(word));
				}
			}
		};
	}

}
